#data for this project were based upon the ANES version "ANES2016TimeSeries_20181218 " with 4270 respondents in the dataset. Code is verified to work on that data.
#On Dataverse we posted an analysis dataset. This dataset can be used to replicate the results reported in the main text and appendix. 
#The R scrip "Study1_US_ANES_recreate_data.R" creates the analysis dataset from the 
rm(list = setdiff(ls(), lsf.str()))

#load analysis dataset
load("Study 1/Original Data (shareable)/Study1_US_ANES_analysisdata.Rdata")

#did not vote
data$didnotvote<-ifelse(data$V162031x==0, 1,
                        ifelse(data$V162031x==1, 0, NA))
#Extraversion--------------------------
#Extraverted - enthusiastic
data$extenth<- car::recode(data$V162333, "-9=NA; -7=NA; -6=NA; -5=NA")
#reserved quiet - reveresed coded
data$reserved_R<- 8-(car::recode(data$V162338, "-9=NA; -7=NA; -6=NA; -5=NA"))
data$extraversion<-rowMeans(data.frame(data$reserved_R, data$extenth))

#Agreeableness-------------------------
#Critical quaralsome - reverse coded
data$critical_R<- 8-(car::recode(data$V162334, "-9=NA; -7=NA; -6=NA; -5=NA"))
#Sympathetci warm
data$symp<- car::recode(data$V162339, "-9=NA; -7=NA; -6=NA; -5=NA")
data$agreeableness<-rowMeans(data.frame(data$critical_R, data$symp))

#Conscientiousness-------------------------
#Disciplined
data$disciplined<- car::recode(data$V162335, "-9=NA; -7=NA; -6=NA; -5=NA")
#Disorganized (recoded)
data$disorganized_R<- 8-(car::recode(data$V162340, "-9=NA; -7=NA; -6=NA; -5=NA"))
#cor.test(data$disciplined, data$disorganized_R)
data$conscientiousness<-rowMeans(data.frame(data$disciplined, data$disorganized_R))

#Neuroticism-------------------------
#Anxiouis
data$anx<- car::recode(data$V162336, "-9=NA; -7=NA; -6=NA; -5=NA")
#calm - reversed
data$calm_R<- 8-(car::recode(data$V162341, "-9=NA; -7=NA; -6=NA; -5=NA"))

#cor.test(data$anx, data$calm_R)
data$neuroticism<-rowMeans(data.frame(data$anx, data$calm_R))

#Openness-------------------------
#new eexperience
data$newexp<- car::recode(data$V162337, "-9=NA; -7=NA; -6=NA; -5=NA")
#uncreative - reversed
data$uncreative_R<- car::recode(data$V162342, "-9=NA; -7=NA; -6=NA; -5=NA")
data$openness<-rowMeans(data.frame(data$uncreative_R+ data$newexp))

#Authoritarianism-------------------------
#independence (0) vs respect(1)
data$auth1<-ifelse(data$V162239==2, 1, 
                   ifelse(data$V162239==1, 0, NA))

#curiosity (0) vs good manners(1)
data$auth2<-ifelse(data$V162240==1, 1, 
                   #ifelse(data$V162240<0, NA, 0))
                   ifelse(data$V162240==2, 0, NA))

#obedience (1) vs self reliance(0)
data$auth3<-ifelse(data$V162241==1, 1, 
                   #ifelse(data$V162241<0, NA, 0))
                   ifelse(data$V162241==2, 0, NA))

#considerate (0) vs well behaved (1)
data$auth4<-ifelse(data$V162242==2, 1, 
                   #ifelse(data$V162242<0, NA, 0))
                   ifelse(data$V162242==1, 0, NA))

data$authoritarianism <- rowMeans(data.frame(data$auth1, data$auth2, data$auth3, data$auth4))

#Covariates-----------------------
#Female
data$female<-ifelse(data$V161342==2, 1, 
                    ifelse(data$V161342==1, 0, NA))

#race
data$black<-ifelse(data$V161310b==1, 1,0)
data$Indian<-ifelse(data$V161310c==1, 1,0)
data$Asian<-ifelse(data$V161310d==1, 1,0)
data$Hawaiian<-ifelse(data$V161310e==1, 1,0)
data$Other<-ifelse(data$V161310f==1, 1,0)

#Education
data$education<-car::recode(data$V161270,"1=1; 2=1; 3=1;4=1;5=1;6=1;7=1;8=1;9=2;10=3;11=4;12=4;13=5;14=6;15=6;16=7;90=8;95=8;-9=NA")

#Age
data$age<-car::recode(data$V161267,"-9=NA; -8=NA")

#Income
data$income<-car::recode(as.numeric(data$V161361x),"1=NA; 2=NA")
data$income<-zero1(data$income)
data$income[is.na(data$income)==TRUE]=2

#income missing
data$income_missing <-ifelse(data$income==2, 1,0)

#Feeling for politicians ---------------------------
#Feeling Trump Pre-election
data$pre_feel_trump<-car::recode(data$V161087,"-99=NA; -88=NA")

#Feeling Clinton Pre-election
data$pre_feel_hillary<-car::recode(data$V161086,"-99=NA; -88=NA")

#Obama Pre-election
data$pre_feel_obama<-car::recode(data$V161092,"-99=NA; -89=NA; -88=NA")

#Bill Clinton Pre-election
data$pre_feel_bill_clinton<-car::recode(data$V161093,"-99=NA; -89=NA; -88=NA")

#Republian Party Feeling Pre-election
data$pre_feel_rep_party<-car::recode(data$V161096,"-99=NA; -89=NA; -88=NA")

#Democratic party feeling pre-election
data$pre_feel_dem_party<-car::recode(data$V161095,"-99=NA; -89=NA; -88=NA")

#Feeling Trump Post-election
data$post_feel_trump<-car::recode(data$V162079,"-99=NA; -89=NA; -88=NA; -9=NA;-8=NA; -7=NA; -6=NA; 998=NA")

#Feeling Hillary Post-election
data$post_feel_hillary<-car::recode(data$V162078, "-99=NA; -89=NA; -88=NA; -9=NA;-8=NA; -7=NA; -6=NA; 998=NA")

#Ideology-----------------
data$post_lib_cons_placement<-car::recode(data$V162171,"99=NA; -9=NA;-8=NA; -7=NA; -6=NA")

#Partisanship-----------------
data$partyidentity<-car::recode(data$V161158x,"-9=NA;-8=NA")

#Trump vote-------------------
data$vote_trump<-ifelse(data$V162034a==2, 1,
                        ifelse(data$V162034a<1, NA, 0))

#Populist, oither, did not vote
data$populist_other_not<- data$vote_trump
data$populist_other_not[data$didnotvote==1]=2

data$edu1<-ifelse(data$education==1, 1,0)
data$edu2<-ifelse(data$education==2, 1,0)
data$edu3<-ifelse(data$education==3, 1,0)
data$edu4<-ifelse(data$education==4, 1,0)
data$edu5<-ifelse(data$education==5, 1,0)
data$edu6<-ifelse(data$education==6, 1,0)
data$edu7<-ifelse(data$education==7, 1,0)
data$edu8<-ifelse(data$education==8, 1,0)

#save in a specified directory
save(data, file="Study 1/Altered Data/Study 1_US_ANES2016.Rdata")

